<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancel-button="null" focus="true"></uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<!-- <uni-icons type="arrowright" size="16"></uni-icon> -->
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史列表 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="16" @click="clean"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 延时器的 timerId
				timer: null,
				// 搜索关键词
				kw: '',
				// 搜索到的结果列表
				searchResults: [],
				// 搜索历史的假数据
				historyList: ['a', 'b', 'v,34244']
			};
		},

		methods: {
			// input输入事件
			input(e) {
				// 清除 timer 对应的延时器
				clearTimeout(this.timer)
				// 重新启动一个延时器，并把 timerId 赋值给 this.timer
				this.timer = setTimeout(() => {
					// 如果 500 毫秒内，没有触发新的输入事件，则为搜索关键词赋值
					this.kw = e
					this.getSearchList()
				}, 500)
			},
			async getSearchList() {
				// 判断搜索关键词是否为空
				if (this.kw.length === 0) {
					this.searchResults = []
					return
				}

				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message

				this.saveSearchHistory()
			},
			gotoDetail(item) {
				console.log(item.goods_id)
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			saveSearchHistory() {
				// this.historyList.push(this.kw)
				// 1.将array数组转为Set
				const set = new Set(this.historyList)
				// 2.删除set中与this.kw相同的元素，确保this.kw在最前面
				set.delete(this.kw)
				// 3.加入kw
				set.add(this.kw)
				// 4.将set对象转为Array
				this.historyList = Array.from(set)
				// 5.持久化到本地
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			clean() {
				// 弹出确认对话框
				uni.showModal({
					title: '提示',
					content: '确定要清空所有搜索历史吗？',
					success: (res) => {
						if (res.confirm) {
							// 用户点击确定
							this.historyList = []
							uni.removeStorageSync('kw')
							uni.showToast({
								title: '已清空'
							})
						} else if (res.cancel) {
							uni.showToast({
								title: '已取消'
							})
						}
					}
				})
			},
			// 点击跳转到商品列表页面
			gotoGoodsList(kw) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		},
		computed: {
			histories() {
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		}
	}
</script>

<style lang="scss">
	.search-box {
		background-color: #C00000;
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid gainsboro;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid gainsboro;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			margin-top: 5px;

			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
				color: black;
				background-color: gainsboro;
				border: 0;
			}
		}

	}
</style>